package demo6;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

//输入18位身份证号码，判断是否成年(满18岁)
//思路二:
public class Adult02 {
    public static void main(String[] args) {
        System.out.println("430124198101070011:" +
                hasAdult("430124198101070011"));// true
        System.out.println("430124202101070011:" +
                hasAdult("430124202101070011"));// false
    }
    /**
     * - 根据身份证号码判断是否成年
     *
     * @param idcard
     * @return
     */
    public static boolean hasAdult(String idcard) {
        if (!idcard.matches("^\\d{17}[\\dX]$")) {
            throw new RuntimeException("18位的身份证号码不正确");
        }
        String ymd = idcard.substring(6, 14);
        System.out.println(ymd);
        DateTimeFormatter f =
                DateTimeFormatter.ofPattern("yyyyMMdd");
        LocalDate d = LocalDate.parse(ymd, f);
        d = d.plusYears(18);

        System.out.println(d);
        return d.isBefore(LocalDate.now());
    }
}
